<?php

namespace app\admin\controller\form4;

use app\admin\model\Form7;
use app\admin\model\TemperatureAlarm;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\Exception;

/**
 * @ControllerAnnotation(title="接触者汇总信息")
 */
class Childen extends AdminController
{

    use \app\admin\traits\Curd;

    protected $relationSearch = true;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Form4Childen();
        
        $this->assign('getSexList', $this->model->getSexList());

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            if (!$this->isSuper) {
                $where[] = ['form4.community', '=', $this->user->community_id];
            }

            $count = $this->model
                ->withJoin('form4')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin('form4')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $row = $this->model->find($this->request->param($this->model->getPk()));
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }
	
	/**
     * @NodeAnotation(title="体温记录")
     */
    public function temperature_index()
    {
        $id = $this->request->param('id/d',0);
        empty($id) && $this->error('提交参数出错');

        $id_card = $this->model->where('id', $id)->value('id_card');

        if (input('selectFields')) {
            return $this->selectList();
        }
        list($page, $limit, $where) = $this->buildTableParames();

        // 关联字段
        $map[] = ['id_card', '=', $id_card];

        if ($this->request->isAjax()) {
            if (input('selectFieds')) {
                return $this->selectList();
            }
            $count = Form7::with(['form4_childen'])
                ->where($where)
                ->where($map)
                ->count();
            $list = Form7::with(['form4_childen'])
                ->where($where)
                ->where($map)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('source_id', $id);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加体温记录")
     */
    public function temperature_add()
    {
        $id = $this->request->param('id/d',0);
        empty($id) && $this->error('提交参数出错');

        $res = $this->model->where('id', $id)->find();
        empty($res) && $this->error('体温对象不存在');

        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'temperature|体温' =>  'require|float|between:30,45',
            ];
            $this->validate($post, $rule);
            try {
                $data = [
                    'name'          =>  $res->name,
                    'id_card'       =>  $res->id_card,
                    'mobile'        =>  $res->mobile,
                    'temperature'   =>  $post['temperature'],
                ];
                if (Form7::whereDay('create_time')->where('id_card', $data['id_card'])->count() >= 2 ){
                    throw new Exception('每天只能提交两次体温');
                }
                //体温报警记录
                if ($post['temperature'] > 37.3) {
                    (new TemperatureAlarm())->save($data);
                }
                Form7::create($data);
            } catch (\Exception $e) {
                $this->error('保存失败：'.$e->getMessage());
            }
            $this->success('保存成功');
        }
        $this->assign('res', $res);
        return $this->fetch();
    }

    
}